//app.js
var fun_md5 = require('pages/utils/md5.js')
import * as Api from './pages/utils/api.js'

App({
  onLaunch: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        that.globalData.windowWidth = res.windowWidth
        that.globalData.windowHeight = res.windowHeight
      }
    })
    that.getToastView()
    // that.doLogin();
  },
  getToastView() {
    var that = this
    wx.getStorage({
      key: 'merchant',
      success: function (res) {
        let merchant = JSON.parse(res.data)
        let url = that.globalData.baseUrl1 + '/statement/query'
        let data = {
          merchantId: merchant.merchantId
        }
        let callBack = (res) => {

          if (res == null) {

            console.log("网络请求错误1！");
            return;
          }

          if (res.data.status != 200) {

            return;
          }

          if (res.data.status == 200) {

            console.log('首页取merchant', res.data)
            if (res.data.data.id) {
              that.globalData.statement = res.data.data
              that.globalData.statementStatus = true
            }
          }
        }
        Api.reqFormPost(url, data, callBack)



      }, fail() {
        that.getToastView()
      }
    })
  },

  doLogin(onLoad, onShow, options) {
    var that = this
    wx.login({
      success: function (res) {
        if (res.code) {
          let url = that.globalData.baseUrl1 + '/wechat/mapp/openid';
          let data = {
            appId: that.globalData.appid,
            code: res.code
          }
          //发起网络请求
          let callBack = (res) => {
            console.log(1, res)
            if (res == null) {
              console.log("网络请求错误！");
              return;
            }
            if (res.statusCode != 200) {

              return;
            }
            // 小程序是否到期
            if (res.data.status != 200) {

              that.globalData.miniAppsEntrance = false,
                that.globalData.miniAppsEntranceMessage = res.data.message
              if (!that.globalData.formStatus) {
                wx.redirectTo({
                  url: '/pages/index/index',
                  success: function (res) { },
                  fail: function (res) { },
                  complete: function (e) {

                  },
                })
              }
  
           
           
           
              return
            }
            that.globalData.merchant = res.data.data.merchant


            let merchantModel = JSON.stringify(res.data.data.merchant)
            wx.setStorageSync('merchant', merchantModel);
       
            wx.setStorageSync('themeColors', res.data.data.merchant.themeColors);
            console.log(res.data.data)
            let openid = res.data.data.openId;
            wx.setStorageSync('openId', openid);//存储openid 
            that.globalData.token = res.data.data.token
            console.log(that.globalData.userId)
            that.globalData.openid = openid
            that.globalData.userId = res.data.data.userId;
            that.globalData.userStatus = res.data.data.userStatus

            
    //如果这里服务到期。跳到首页


            //每个页面初始化的方法
            onLoad(options);
            onShow();

            //更新用户信息
            wx.getUserInfo({
              success: (res2) => {
                that.globalData.userInfo = res2.userInfo

                if (!that.globalData.userId) {
                  console.log('不存在更新行为')
                  return
                }
                console.log('更新前的用户信息', that.globalData.userInfo)


           

                let url = that.globalData.baseUrl1 + '/user/manager/update';
                let data = {
                  token: that.globalData.token,
                  userId: that.globalData.userId,
                  iconUrl: that.globalData.userInfo.avatarUrl,
                  nickName: that.globalData.userInfo.nickName
                }
                let callBack = (res) => {
                  console.log(555)

                  if (res == null) {
                    console.log("网络请求错误！");
                    return;
                  }
                  if (res.data.status != 200) {
                    console.log("请求数据错误11");
                    return;
                  }

                }
                Api.reqFormPost(url, data, callBack)

              }
            })




          }
          Api.reqFormPost(url, data, callBack)

        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }, fail(e) {
        console.log()
        return
      }
    });

  },



  // 第一次 没设置车型   设置完  返回来  继续设置一遍   会返回到首页
  globalData: {

    carId: null,//临时选择的车型ID


    merchant: null,

    appid: 'wx16ca0d17ce2a9ace',
    token: null,
    windowWidth: 375,
    windowHeight:'',
    isClick: true,
    lastTapTime: 0,
    userInfo: null,
    userId: null,
    openid: "",
    formStatus:false,
    miniAppsEntrance: true,//服务开通状态
    miniAppsEntranceMessage: "",//错误信息

    statement: null,//通知内容
    statementStatus: false,//通知状态


    registerStatus: null, //用户是否注册
    userStatus: null, //用户审核状态

    mustLogin: null,//用户是否强制登录

    vinSearch: null,//是否开通VIN查询

    basicMall: null,//是否开通基础商城
    seniorMall: null,//是否开通
    baseUrl: "https://api-merchant.nanxinwang.com/sapi",
    baseUrl1: "https://api-merchant.nanxinwang.com/bapi",
 

  }
})
